<template>
  <ZZDialog class="analysisBox" title="空间分析" @close="callbackClose()">
    <div class="basicbox" v-if="currentType == ''">
      <div class="btn-group">
        <div id="btn_goto_rzfx" class="tool-btn" @click="analysisType('rzfx')">
          <div class="tool-thum" style="background: #dd751b"><img src="./img/rzfx.svg" alt="日照分析" /></div>
          <span class="btn_none">日照分析</span>
        </div>

        <div id="btn_goto_ksy" class="tool-btn" @click="analysisType('ksy')">
          <div class="tool-thum" style="background: #c092fe"><img src="./img/ksy.svg" alt="可视域" /></div>
          <span class="btn_none">可视域</span>
        </div>

        <div id="btn_goto_flfx" class="tool-btn" @click="analysisType('flfx')">
          <div class="tool-thum" style="background: #88b8ff"><img src="./img/flfx.svg" alt="方量分析" /></div>
          <span class="btn_none">方量分析</span>
        </div>

        <div id="btn_goto_dxkw" class="tool-btn" @click="analysisType('dxkw')">
          <div class="tool-thum" style="background: #55d5a0"><img src="./img/dxkw.svg" alt="地形开挖" /></div>
          <span class="btn_none">地形开挖</span>
        </div>

        <div id="btn_goto_dbtm" class="tool-btn" @click="analysisType('dbtm')">
          <div class="tool-thum" style="background: #b85555"><img src="./img/dbtm.svg" alt="地表透明" /></div>
          <span class="btn_none">地表透明</span>
        </div>

        <div id="btn_goto_pdpx" class="tool-btn" @click="analysisType('pdpx')">
          <div class="tool-thum" style="background: #3de3f4"><img src="./img/pdpx.svg" alt="坡度坡向" /></div>
          <span class="btn_none">坡度坡向</span>
        </div>

        <div id="btn_goto_mxpq" class="tool-btn" @click="analysisType('mxpq')">
          <div class="tool-thum" style="background: #37bc41"><img src="./img/mxpq.svg" alt="模型剖切" /></div>
          <span class="btn_none">模型剖切</span>
        </div>

        <div id="btn_goto_mxyp" class="tool-btn" @click="analysisType('mxyp')">
          <div class="tool-thum" style="background: #95d333"><img src="./img/mxyp.svg" alt="模型压平" /></div>
          <span class="btn_none">模型压平</span>
        </div>

        <div id="btn_goto_mxcj" class="tool-btn" @click="analysisType('mxcj')">
          <div class="tool-thum" style="background: #babc31"><img src="./img/mxcj.svg" alt="模型裁剪" /></div>
          <span class="btn_none">模型裁剪</span>
        </div>
      </div>
    </div>

    <!-- 日照分析 -->
    <div id="sunAnal" class="mainbox" v-if="currentType === 'rzfx'">
      <div class="headbox">
        <div class="headtitle">
          <i id="btn_rzfx_destory" class="fa fa-chevron-left backmenu" @click="destroyRZFX"></i>
          <span>日照分析</span>
        </div>
        <div id="btn_rzfx_clear" class="del" @click="destroyRZFX">
          <i class="fa fa-trash-o"></i>
          <span>清除</span>
        </div>
      </div>
      <div class="mainInfo">
        <div class="rowview clearfix tip">提示:模拟设定时间范围内的太阳光照效果。</div>
        <div class="rowview clearfix">
          <span>日期选择:</span>
          <el-date-picker v-model="rzfx_Date" type="date" placeholder="选择日期" style="width: 200px" />
        </div>
        <div class="rowview clearfix">
          <span>开始时间:</span>
          <el-time-picker v-model="rzfx_StartTime" placeholder="开始时间" style="width: 200px" />
        </div>
        <div class="rowview clearfix">
          <span>结束时间:</span>
          <el-time-picker v-model="rzfx_EndTime" placeholder="结束时间" style="width: 200px" />
        </div>
        <div class="rowview clearfix center">
          <el-button @click="startRZFX(rzfx_StartTime, rzfx_EndTime)">播放</el-button>
          <el-button @click="stopRZFX()">暂停</el-button>
        </div>
        <div class="rowview clearfix">
          <span>当前时间:</span>
          <span id="lbl_rzfx_nowTime">{{ currentTime }}</span>
        </div>
      </div>
    </div>

    <!-- 可视域 -->
    <div id="ksyAnal" class="mainbox" v-if="currentType == 'ksy'">
      <div class="headbox">
        <div class="headtitle">
          <i id="btn_ksy_destory" class="fa fa-chevron-left backmenu" @click="backAnalysis"></i>
          <span>可视域</span>
        </div>
        <div id="btn_ksy_clear" class="del" @click="destoryAnalysis">
          <i class="fa fa-trash-o" @click="destoryAnalysis"></i>
          <span>清除</span>
        </div>
      </div>
      <div class="mainInfo">
        <div class="rowview clearfix tip">提示:单击按钮后在图上绘制，红色代表不可视，绿色代表可视。</div>

        <div class="rowview clearfix">
          <span>水平张角:</span>
          <el-slider v-model="ksy_horizontalAngle" :max="120" :min="1" show-input size="small" style="width: 200px" />
        </div>
        <div class="rowview clearfix">
          <span>垂直张角:</span>
          <el-slider v-model="ksy_verticalAngle" :max="90" :min="1" show-input size="small" style="width: 200px" />
        </div>
        <div class="rowview clearfix">
          <span>视角距离:</span>
          <el-slider v-model="ksy_distance" :max="5000" :min="1" show-input size="small" style="width: 200px" />
        </div>
        <div class="rowview clearfix">
          <span>视椎框线:</span>
          <div class="checkbox checkbox-info checkbox-inline">
            <el-checkbox v-model="ksy_DebugFrustum" label="显示框线" size="large" />
          </div>
        </div>
        <div class="rowview clearfix center">
          <el-button style="margin-bottom: 10px" @click="startKSY">添加可视域</el-button>
        </div>
      </div>
    </div>

    <!-- 方量分析 -->
    <div id="flAnal" class="mainbox" v-if="currentType == 'flfx'">
      <div class="headbox">
        <div class="headtitle">
          <i id="btn_flfx_destory" class="fa fa-chevron-left backmenu" @click="backAnalysis"></i>
          <span>方量分析</span>
        </div>
        <div id="btn_flfx_clear" class="del" @click="destoryAnalysis">
          <i class="fa fa-trash-o" @click="destoryAnalysis"></i>
          <span>清除</span>
        </div>
      </div>
      <div class="mainInfo">
        <div class="rowview clearfix tip">提示:请首先单击 “绘制区域” 按钮，再在图上绘制分析区域。</div>

        <div class="rowview clearfix center">
          <el-button @click="startFLFX">
            <i class="fa fa-edit"></i>
            绘制分析区域
          </el-button>
        </div>
        <div class="rowview clearfix">
          <span>基准面高:</span>
          <el-tooltip content="计量单位：米" placement="right">
            <el-input-number
              controls-position="right"
              v-model="flfx_Height"
              @change="heightChange('h')"
              size="small"
              :min="1"
              :max="100"
              :step="0.1"
            />
          </el-tooltip>
          <el-button size="small" style="margin-left: 10px" @click="pickHeight">点选高度</el-button>
        </div>

        <div class="rowview clearfix">
          <span>围墙底高:</span>
          <el-tooltip content="计量单位：米" placement="right">
            <el-input-number
              controls-position="right"
              v-model="flfx_MinHeight"
              @change="heightChange('min')"
              size="small"
              :min="1"
              :max="100"
              :step="0.1"
            />
          </el-tooltip>
          （可选）
        </div>
        <div class="rowview clearfix" style="margin-bottom: 10px">
          <span>围墙顶高:</span>
          <el-tooltip content="计量单位：米" placement="right">
            <el-input-number
              controls-position="right"
              v-model="flfx_MaxHeight"
              @change="heightChange('max')"
              size="small"
              :min="1"
              :max="100"
              :step="0.1"
            />
          </el-tooltip>
          （可选）
        </div>
      </div>
    </div>

    <!-- 地形开挖 -->
    <div id="dxAnal" class="mainbox" v-if="currentType == 'dxkw'">
      <div class="headbox">
        <div class="headtitle">
          <i id="btn_dxkw_destory" class="fa fa-chevron-left backmenu" @click="backAnalysis"></i>
          <span>地形开挖</span>
        </div>
        <div id="btn_dxkw_clear" class="del" @click="destoryAnalysis">
          <i class="fa fa-trash-o" @click="destoryAnalysis"></i>
          <span>清除</span>
        </div>
      </div>
      <div class="mainInfo">
        <div class="rowview clearfix tip">提示:请首先单击 “绘制区域” 按钮，再在图上绘制开挖区域。</div>
        <div class="rowview clearfix">
          <span>开挖深度:</span>
          <el-tooltip content="计量单位：米" placement="right">
            <el-input-number
              controls-position="right"
              v-model="dxkw_clipHeight"
              size="small"
              :min="1"
              :max="100"
              :step="1"
            />
          </el-tooltip>
        </div>
        <div class="rowview clearfix center">
          <el-button style="margin-bottom: 10px" @click="startDXKW">
            <i class="fa fa-edit"></i>
            绘制开挖矩形
          </el-button>
        </div>
      </div>
    </div>

    <!-- 地表透明 -->
    <div id="dbAnal" class="mainbox" v-if="currentType == 'dbtm'">
      <div class="headbox">
        <div class="headtitle">
          <i id="btn_dbtm_destory" class="fa fa-chevron-left backmenu" @click="backAnalysis"></i>
          <span>地表透明</span>
        </div>
        <div id="btn_dbtm_clear" class="del" @click="destoryAnalysis">
          <i class="fa fa-trash-o" @click="destoryAnalysis"></i>
          <span>清除</span>
        </div>
      </div>
      <div class="mainInfo">
        <div class="rowview clearfix tip">提示:可以透明地表，进入地下模式，可以看地下管网等数据。</div>

        <div class="rowview clearfix">
          <span>开启状态:</span>
          <el-checkbox v-model="dbtm_Underground" label="开启地表透明" size="large" @change="changeDBTM" />
        </div>

        <div class="rowview clearfix" style="margin-bottom: 5px">
          <span>地表透明度:</span>
          <el-slider
            v-model="dbtm_alpha"
            :max="1.0"
            :min="0.0"
            :step="0.1"
            show-input
            size="small"
            style="width: 190px"
          />
        </div>
      </div>
    </div>

    <!-- 坡度坡向 -->
    <div id="tsAnal" class="mainbox" v-if="currentType == 'pdpx'">
      <div class="headbox">
        <div class="headtitle">
          <i id="btn_pdpx_destory" class="fa fa-chevron-left backmenu" @click="backAnalysis"></i>
          <span>坡度坡向</span>
        </div>
        <div id="btn_pdpx_clear" class="del" @click="destoryAnalysis">
          <i class="fa fa-trash-o" @click="destoryAnalysis"></i>
          <span>清除</span>
        </div>
      </div>
      <div class="mainInfo">
        <div class="rowview clearfix tip">提示:单击按钮后在图上绘制区域。</div>
        <div class="rowview clearfix">
          插值数:
          <el-tooltip content="计量单位：米" placement="right">
            <el-input-number
              controls-position="right"
              style="width: 200px"
              v-model="pdpx_SplitNum"
              size="small"
              :min="1"
              :max="9999"
              :step="1"
            />
          </el-tooltip>
        </div>
        <div class="rowview clearfix center" style="margin-bottom: 10px">
          <el-button style="padding: auto" @click="startPDPX">绘制区域</el-button>
        </div>
      </div>
    </div>

    <!-- 模型剖切 -->
    <div id="mxpqAnal" class="mainbox" v-if="currentType == 'mxpq'">
      <div class="headbox">
        <div class="headtitle">
          <i id="btn_mxpq_destory" class="fa fa-chevron-left backmenu" @click="backAnalysis"></i>
          <span>模型剖切</span>
        </div>
        <div id="btn_mxpq_clear" class="del" @click="destoryAnalysis">
          <i class="fa fa-trash-o" @click="destoryAnalysis"></i>
          <span>清除</span>
        </div>
      </div>
      <div class="mainInfo">
        <div class="rowview clearfix tip">提示:请先图上点选模型后，设置剖切方向及距离。</div>
        <div class="rowview clearfix">
          <span>选择模型:</span>
          <el-button @click="select_mxpq_tiles()">图上点选</el-button>
          <span id="lbl_mxpq_mxmc">{{ mxpq_tile }}</span>
        </div>
        <div class="rowview clearfix">
          <span>按线剖切:</span>
          <el-button @click="draw_mxpq_line()">绘制线</el-button>
        </div>
        <div class="rowview clearfix">
          <span>剖切方向:</span>
          <div style="margin-left: 58px">
            <el-button @click="startMXPQ('ding')">顶</el-button>
            <el-button @click="startMXPQ('di')">底</el-button>
            <el-button @click="startMXPQ('dong')">东</el-button>
            <el-button @click="startMXPQ('nan')">南</el-button>
            <el-button @click="startMXPQ('xi')">西</el-button>
            <el-button @click="startMXPQ('bei')">北</el-button>
          </div>
        </div>
        <div class="rowview clearfix">
          <span>剖切距离:</span>
          <el-slider
            v-model="mxpq_Distance"
            @input="changeMXPQ()"
            :max="100"
            :min="-100"
            :step="1"
            show-input
            size="small"
            style="width: 190px"
          />
        </div>
      </div>
    </div>

    <!-- 模型压平 -->
    <div id="mxypAnal" class="mainbox" v-if="currentType == 'mxyp'">
      <div class="headbox">
        <div class="headtitle">
          <i id="btn_mxyp_destory" class="fa fa-chevron-left backmenu" @click="backAnalysis"></i>
          <span>模型压平</span>
        </div>
        <div id="btn_mxyp_clear" class="del" @click="destoryAnalysis">
          <i class="fa fa-trash-o" @click="destoryAnalysis"></i>
          <span>清除</span>
        </div>
      </div>
      <div class="mainInfo">
        <div class="rowview clearfix tip">提示: 模型压平只支持部分无着色器的3dtiles数据。</div>
        <div class="rowview clearfix">
          <span>选择模型:</span>
          <el-button @click="select_mxyp_tile()">图上点选</el-button>
          <span id="lbl_mxpq_mxmc">{{ mxyp_tile }}</span>
        </div>
        <div class="rowview clearfix">
          <span>压平高度:</span>
          <el-tooltip content="计量单位：米" placement="right">
            <el-input-number
              controls-position="right"
              style="width: 140px"
              v-model="mxyp_flatHeight"
              size="small"
              :min="0"
              :max="100"
              :step="1"
            />
          </el-tooltip>
        </div>

        <div class="rowview clearfix center">
          <el-button style="margin-bottom: 10px" @click="startMXYP()">
            <i class="fa fa-edit"></i>
            绘制压平区域
          </el-button>
        </div>
      </div>
    </div>

    <!-- 模型裁剪 -->
    <div id="mxcjAnal" class="mainbox" v-if="currentType == 'mxcj'">
      <div class="headbox">
        <div class="headtitle">
          <i id="btn_mxcj_destory" class="fa fa-chevron-left backmenu" @click="backAnalysis"></i>
          <span>模型裁剪</span>
        </div>
        <div id="btn_mxcj_clear" class="del" @click="destoryAnalysis">
          <i class="fa fa-trash-o" @click="destoryAnalysis"></i>
          <span>清除</span>
        </div>
      </div>
      <div class="mainInfo">
        <div class="rowview clearfix tip">提示: 模型裁剪只支持部分无着色器的3dtiles数据。</div>

        <div class="rowview clearfix">
          <span>选择模型:</span>
          <el-button @click="select_mxcj_tile()">图上点选</el-button>
          <span id="lbl_mxpq_mxmc">{{ mxcj_tile }}</span>
        </div>

        <div class="rowview clearfix">
          <span>裁剪方式:</span>
          <el-radio-group v-model="mxcj_Type">
            <el-radio :label="0">内裁剪</el-radio>
            <el-radio :label="1">外裁剪</el-radio>
          </el-radio-group>
        </div>
        <div class="rowview clearfix center" style="padding-bottom: 10px">
          <el-button @click="startMXCJ">
            <i class="fa fa-edit"></i>
            绘制裁剪区域
          </el-button>
        </div>
      </div>
    </div>
  </ZZDialog>
</template>

<script setup lang="ts">
import { ref, onBeforeMount, onMounted } from 'vue';
import { useViewer } from '@/stores/viewer';
import ZZDialog from '@/common/ZZDialog.vue';
import { ElMessage } from 'element-plus';
import wallimg from './img/textures/excavationregion_side.jpg';
import topimg from './img/textures/excavationregion_top.jpg';
let viewer: any; //场景视图
const currentType = ref('');
let currentAnalysisObj: any = undefined;

const rzfx_Date = ref(new Date(2023, 11, 22));
const rzfx_StartTime = ref(new Date(2023, 12, 22, 8, 0));
const rzfx_EndTime = ref(new Date(2023, 12, 22, 18, 0));
const currentTime = ref('');

const ksy_horizontalAngle = ref(90);
const ksy_verticalAngle = ref(60);
const ksy_distance = ref(10);
const ksy_DebugFrustum = ref(true);

const flfx_Height = ref(0);
const flfx_MinHeight = ref(0);
const flfx_MaxHeight = ref(0);
const flfx_chkUnderground = ref(false);

const dxkw_clipHeight = ref(1);

const dbtm_alpha = ref(0.5);
const dbtm_Underground = ref(false);

const pdpx_SplitNum = ref(10);

const mxpq_Distance = ref(0);
const mxpq_tile = ref('未选择');

const mxyp_flatHeight = ref(0);
const mxyp_tile = ref('未选择');

const mxcj_Type = ref(0);
const mxcj_tile = ref('未选择');

onBeforeMount(() => {
  viewer = useViewer().viewer;
});

onMounted(() => {
  currentTime.value = getcurrentTime();
});

function analysisType(type: string) {
  currentType.value = type;
}

function backAnalysis() {
  destoryAnalysis();
  currentType.value = '';
}

// 日照分析
let stopTime: null = null;
function startRZFX(startDate: any, endDate: any) {
  viewer.clock.shouldAnimate = true;
  if (stopTime) {
    viewer.clock.currentTime = stopTime;
  } else {
    viewer.scene.globe.enableLighting = true;
    viewer.shadows = true;
    viewer.clock.startTime = Cesium.JulianDate.fromDate(startDate);
    viewer.clock.currentTime = Cesium.JulianDate.fromDate(startDate);
    viewer.clock.stopTime = Cesium.JulianDate.fromDate(endDate);
    viewer.clock.clockRange = Cesium.ClockRange.LOOP_STOP;
    viewer.clock.clockStep = Cesium.ClockStep.SYSTEM_CLOCK_MULTIPLIER;
    viewer.clock.multiplier = 1600;
  }
}

function stopRZFX() {
  stopTime = viewer.clock.currentTime;
  viewer.clock.shouldAnimate = false;
}

function destroyRZFX() {
  rzfx_Date.value = new Date(2023, 11, 22);
  rzfx_StartTime.value = new Date(2023, 12, 22, 8, 0);
  rzfx_EndTime.value = new Date(2023, 12, 22, 18, 0);
  currentTime.value = '';
  currentType.value = '';

  viewer.clock.clockRange = Cesium.ClockRange.UNBOUNDED;
  viewer.clock.multiplier = 1;
  viewer.shadows = false;
}

// 可视域
function startKSY() {
  destoryAnalysis();
  viewer.zz.analyseFactory
    .create('ViewShed', {
      horizontalAngle: ksy_horizontalAngle.value,
      verticalAngle: ksy_verticalAngle.value,
      distance: ksy_distance.value,
      debugFrustum: ksy_DebugFrustum.value,
      offsetHeight: 1.5 //加人的身高等因素，略微抬高一些
    })
    .then((id: string) => {
      console.log('当前分析为：可视域分析');
      currentAnalysisObj = viewer.zz.analyseFactory.get(id);
      ksy_distance.value = currentAnalysisObj?._options?.distance || 10.0;
    })
    .catch(() => console.log('cancel'));
}
function getcurrentTime() {
  const getTime = new Date().getTime();
  const time = new Date(getTime);
  const year = time.getFullYear(); // 年
  const month = (time.getMonth() + 1).toString().padStart(2, '0'); // 月
  const date = time.getDate().toString().padStart(2, '0'); // 日
  const hour = time.getHours().toString().padStart(2, '0'); // 时
  const minute = time.getMinutes().toString().padStart(2, '0'); // 分
  const second = time.getSeconds().toString().padStart(2, '0'); // 秒
  return year + '-' + month + '-' + date + ' ' + hour + ':' + minute + ':' + second;
}

// 方量分析
function startFLFX() {
  destoryAnalysis();
  viewer.zz.analyseFactory
    .create('MeasureVolume', {
      splitNum: 6, //面内插值次数，控制精度[注意精度越大，分析时间越长]
      onStart: () => {},
      onEnd: () => {
        flfx_Height.value = Number(currentAnalysisObj.height.toFixed(2));
        flfx_MinHeight.value = Number(currentAnalysisObj.minHeight.toFixed(2));
        flfx_MaxHeight.value = Number(currentAnalysisObj.maxHeight.toFixed(2));
      },
      style: { color: '#FFFF00' }
    })
    .then((id: string) => {
      console.log('当前分析为：方量分析');
      currentAnalysisObj = viewer.zz.analyseFactory.get(id);
    })
    .catch(() => console.log('cancle'));
}
function heightChange(type: string) {
  if ((currentAnalysisObj = undefined)) {
    return;
  }
  switch (type) {
    case 'h':
      currentAnalysisObj.height = flfx_Height.value;
      break;
    case 'min':
      if (flfx_MinHeight.value > Number(currentAnalysisObj.height)) {
        ElMessage({
          message: '墙底部高度不能高于基准面高',
          type: 'error'
        });
      }
      currentAnalysisObj.minHeight = flfx_MinHeight.value;
      break;
    case 'max':
      if (flfx_MaxHeight.value < Number(currentAnalysisObj.height)) {
        ElMessage({
          message: '墙顶部高度不能低于基准面高',
          type: 'error'
        });
      }
      currentAnalysisObj.maxHeight = flfx_MaxHeight.value;
      break;
    default:
      break;
  }
}
function pickHeight() {
  if (!currentAnalysisObj) {
    return;
  }
  currentAnalysisObj.selecteHeight(() => {
    flfx_Height.value = Number(currentAnalysisObj.height.toFixed(2));
    flfx_MinHeight.value = Number(currentAnalysisObj.minHeight.toFixed(2));
    flfx_MaxHeight.value = Number(currentAnalysisObj.maxHeight.toFixed(2));
  });
}

// 地形开挖
function startDXKW() {
  destoryAnalysis();
  currentAnalysisObj = new zz3d.analysi.TerrainClipEx(viewer, {
    height: dxkw_clipHeight.value,
    splitNum: 50, //wall边界插值数
    wallImg: wallimg,
    bottomImg: topimg
  });
  console.log('当前分析为：地形开挖');
}

// 地表透明
function changeDBTM() {
  currentAnalysisObj = new zz3d.analysi.Underground(viewer, {
    alpha: dbtm_alpha.value,
    enable: dbtm_Underground.value
  });
  if (dbtm_Underground.value) console.log('当前分析为：地形开挖');
}

// 坡度坡向
function startPDPX() {
  destoryAnalysis();
  viewer.zz.analyseFactory
    .create('Slope', {
      type: 'polygon', //进行绘制面操作
      style: {
        //绘制面样式
        color: '#29cf34',
        opacity: 0.3,
        clampToGround: true
      },
      onStart: () => {}, //分析开始回调
      onEnd: () => {}, //分析结束回调
      point: {
        //分析结果中分析基点样式
        show: true,
        pixelSize: 9,
        color: Cesium.Color.RED.withAlpha(0.5)
      },
      arrow: {
        //分析结果中箭头样式
        show: true,
        scale: 0.3, //箭头长度的比例（范围0.1-0.9）
        width: 15, //箭头宽度
        color: Cesium.Color.YELLOW
      }
    })
    .then((id: any) => {
      console.log('当前分析为：坡度坡向');
      currentAnalysisObj = viewer.zz.analyseFactory.get(id);
    })
    .catch((_e: any) => {
      console.log('cancel');
    });
}

// 模型刨切
function startMXPQ(type: string) {
  if (!currentAnalysisObj) {
    window.alert('请选择模型');
    return;
  }
  switch (type) {
    case 'ding':
      currentAnalysisObj.type = zz3d.tiles.TilesClipPlan.Type.Z;
      break;
    case 'di':
      currentAnalysisObj.type = zz3d.tiles.TilesClipPlan.Type.ZR;
      break;
    case 'dong':
      currentAnalysisObj.type = zz3d.tiles.TilesClipPlan.Type.X;
      break;
    case 'nan':
      currentAnalysisObj.type = zz3d.tiles.TilesClipPlan.Type.XR;
      break;
    case 'xi':
      currentAnalysisObj.type = zz3d.tiles.TilesClipPlan.Type.Y;
      break;
    case 'bei':
      currentAnalysisObj.type = zz3d.tiles.TilesClipPlan.Type.YR;
      break;

    default:
      break;
  }
}
function draw_mxpq_line() {
  viewer.zz.draw.startDraw({
    type: 'polyline',
    config: {
      maxPointNum: 2
    },
    style: {
      color: '#007be6',
      opacity: 0.8,
      outline: false
    },
    success: (entity: any) => {
      //绘制成功后回调
      const points = viewer.zz.draw.getPositions(entity);
      viewer.zz.draw.deleteAll();
      if (!currentAnalysisObj) {
        window.alert('请先选中模型');
        return;
      }
      currentAnalysisObj.clipByPoints(points);
    }
  });
}
function select_mxpq_tiles() {
  viewer.zz.draw.startDraw({
    type: 'point',
    style: {
      color: '#007be6'
    },
    success: (entity: any) => {
      //绘制成功后回调
      const positions = viewer.zz.draw.getPositions(entity);
      viewer.zz.draw.deleteAll();
      const tileset = zz3d.tileset.pick3DTileset(viewer, positions); //拾取绘制返回的模型
      if (!tileset) {
        window.alert('请选择模型');
        return;
      }
      currentAnalysisObj = new zz3d.tiles.TilesClipPlan(tileset);
      mxpq_tile.value = currentAnalysisObj._tileset.name;
    }
  });
}
function changeMXPQ() {
  if (!currentAnalysisObj) {
    return;
  }
  currentAnalysisObj.distance = mxpq_Distance.value;
}

// 模型压平
function startMXYP() {
  viewer.zz.draw.deleteAll();
  viewer.zz.draw.startDraw({
    type: 'polygon',
    style: {
      color: '#007be6',
      opacity: 0.5,
      clampToGround: false
    },
    success: (entity: any) => {
      //绘制成功后回调
      const positions = viewer.zz.draw.getPositions(entity);
      //显示边界线处理
      const coords = viewer.zz.draw.getCoordinates(entity);
      coords.push(coords[0]);
      viewer.zz.draw.deleteAll();
      viewer.zz.draw.addPolyline(coords, {
        color: '#ffffff',
        clampToGround: true,
        width: 2
      });
      currentAnalysisObj.addArea(positions, undefined, mxyp_flatHeight.value);
    }
  });
}
function select_mxyp_tile() {
  viewer.zz.draw.startDraw({
    type: 'point',
    style: {
      color: '#007be6'
    },
    success: (entity: any) => {
      //绘制成功后回调
      const positions = viewer.zz.draw.getPositions(entity);
      viewer.zz.draw.deleteAll();
      const tileset = zz3d.tileset.pick3DTileset(viewer, positions); //拾取绘制返回的模型
      if (!tileset) {
        window.alert('请选择模型');
        return;
      }
      currentAnalysisObj = new zz3d.tiles.createFlatObj({
        viewer: viewer,
        tileset: tileset
      });
      mxyp_tile.value = tileset.name;
    }
  });
}

// 模型裁剪
function startMXCJ() {
  viewer.zz.draw.startDraw({
    type: 'polygon',
    style: {
      color: '#007be6',
      opacity: 0.5,
      clampToGround: true
    },
    success: function (entity: any) {
      //绘制成功后回调
      const points = viewer.zz.draw.getPositions(entity);
      viewer.zz.draw.deleteAll();

      currentAnalysisObj.clipByPoints(points, {
        unionClippingRegions: mxcj_Type.value === 0 ? false : true,
        edgeColor: Cesium.Color.GREY,
        edgeWidth: 2.0
      });
    }
  });
}
function select_mxcj_tile() {
  viewer.zz.draw.startDraw({
    type: 'point',
    style: {
      color: '#007be6'
    },
    success: (entity: any) => {
      //绘制成功后回调
      const positions = viewer.zz.draw.getPositions(entity);
      viewer.zz.draw.deleteAll();
      const tileset = zz3d.tileset.pick3DTileset(viewer, positions); //拾取绘制返回的模型
      if (!tileset) {
        window.alert('请选择模型');
        return;
      }
      currentAnalysisObj = new zz3d.tiles.TilesClipPlan(tileset);
      mxcj_tile.value = currentAnalysisObj._tileset.name;
    }
  });
}
// 注销分析对象
function destoryAnalysis() {
  viewer.zz.draw.deleteAll();
  if (currentAnalysisObj) {
    switch (currentType.value) {
      case 'dxkw':
        currentAnalysisObj.destroy();
        break;
      case 'dbtm':
        currentAnalysisObj.enable = false;
        break;
      case 'mxpq':
        currentAnalysisObj.clear();
        break;
      case 'mxyp':
        currentAnalysisObj.destroy();
        break;
      case 'mxcj':
        currentAnalysisObj.clear();
        break;
      default:
        viewer.zz.analyseFactory.remove(currentAnalysisObj?.id);
        viewer.zz.analyseFactory.removeAll();
        break;
    }
  }
  currentAnalysisObj = undefined;

  ksy_horizontalAngle.value = 90;
  ksy_verticalAngle.value = 60;
  ksy_distance.value = 10;
  ksy_DebugFrustum.value = true;

  flfx_Height.value = 0;
  flfx_MinHeight.value = 0;
  flfx_MaxHeight.value = 0;
  flfx_chkUnderground.value = false;

  dxkw_clipHeight.value = 1;

  dbtm_Underground.value = false;
}

const callbackClose = function () {};
</script>

<style scoped lang="less">
@import 'index.css';
.analysisBox {
  position: absolute;
  width: 300px;
  right: 105px;
  top: 50px;
}
:deep .el-slider__runway.show-input {
  margin-right: 10px !important;
}
:deep .el-slider__input {
  width: 80px !important;
}
</style>
